﻿using Knight.Common.Security;
using Knlght.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Knight.Models.Services
{
    /// <summary>
    /// 客户端服务处理基类
    /// </summary>
    public abstract class ServiceBase
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        protected string Encipher(string password)
        {
            return DES.DesEncrypt(password, Common.DConfig.DESKEY);
        }

        #region 数据操作方法
        /// <summary>
        /// 执行操作
        /// </summary>
        protected ActionResponse doAction(Func<bool> exec)
        {
            if (exec == null)
                throw new ArgumentNullException("exec");

            try
            {
                var result = exec();

                if (result)
                {
                    return ActionResponse.CreateResponseBySuccess();
                }
                else
                {
                    return ActionResponse.CreateResponseByFail("操作失败!");
                }
            }
            catch (EJApplicationException ex)
            {
                if (ex.ExceptionCode == ResultCodeConstants.ERROR_NOAUTHORIZE)
                {
                    return ActionResponse.CreateResponseByNotAuthorize();
                }
                else
                {
                    return ActionResponse.CreateResponseByFail(ex.Message, ex.ExceptionCode);
                }
            }
            catch (Exception ex)
            {
                var msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                return ActionResponse.CreateResponseByFail("错误:" + msg);
            }
        }
        /// <summary>
        /// 获取单个对象模型方法
        /// </summary>
        protected ItemResponse<T> GetModel<T>(Func<T> exec)
        {
            if (exec == null)
                throw new ArgumentNullException("exec");
            try
            {
                var model = exec();
                if (model == null)
                {
                    return ItemResponse<T>.CreateResponseByFail("获取失败!", ResultCodeConstants.ERROR_NOTFIND);
                }
                else
                {
                    return ItemResponse<T>.CreateResponseBySuccess(model);
                }
            }
            catch (EJApplicationException ex)
            {
                return ItemResponse<T>.CreateResponseByFail(ex.Message, ex.ExceptionCode);
            }
            catch (Exception ex)
            {
                var msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                return ItemResponse<T>.CreateResponseByFail("错误:" + msg);
            }
        }
        /// <summary>
        /// 获取对象集合
        /// </summary>
        protected ListResponse<T> GetList<T>(Func<List<T>> exec)
        {
            if (exec == null)
                throw new ArgumentNullException("exec");

            try
            {
                var list = exec();
                if (list == null)
                {
                    return ListResponse<T>.CreateResponseByFail("获取失败!", ResultCodeConstants.ERROR_NOTFIND);
                }
                else
                {
                    return ListResponse<T>.CreateResponseBySuccess(list);
                }
            }
            catch (EJApplicationException ex)
            {
                return ListResponse<T>.CreateResponseByFail(ex.Message, ex.ExceptionCode);
            }
            catch (Exception ex)
            {
                var msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                return ListResponse<T>.CreateResponseByFail("错误:" + msg);
            }
        }
        /// <summary>
        /// 分页获取对象集合
        /// </summary>
        protected PagingResponse<T> GetPaging<T>(Func<PagingList<T>> exec)
        {
            if (exec == null)
                throw new ArgumentNullException("exec");

            try
            {
                var result = exec();
                if (result == null)
                {
                    return PagingResponse<T>.CreateResponseByFail("获取失败!", ResultCodeConstants.ERROR_NOTFIND);
                }
                else
                {
                    return PagingResponse<T>.CreateResponseBySuccess(result.Models, result.TotalModels, result.TotalPages);
                }
            }
            catch (EJApplicationException ex)
            {
                return PagingResponse<T>.CreateResponseByFail(ex.Message, ex.ExceptionCode);
            }
            catch (Exception ex)
            {
                var msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                return PagingResponse<T>.CreateResponseByFail("错误:" + msg);
            }
        }
        #endregion
    }
}
